package me.macd.mindoc.dao.impl;

import me.macd.mindoc.dao.LabelDao;
import me.macd.mindoc.model.BookResult;
import me.macd.mindoc.model.Label;
import me.macd.mindoc.util.DefaultNameHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-10-10
 * @create: 2018-10-10 20:37
 */
@Repository
public class LabelDaoJDBCTemplateImpl implements LabelDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public LabelDaoJDBCTemplateImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int countToPager() {
        String sql = "SELECT COUNT(*) FROM md_label";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count == null ? 0 : count;
    }

    @Override
    public List<Label> findToPager(int start, int pageSize) {
        String sql = "select label_id, label_name, book_number from md_label" +
                " order by book_number DESC LIMIT ?,?";
        return jdbcTemplate.query(sql, new Object[]{start, pageSize}, new CommonRowMapper<>(Label.class, new DefaultNameHandler()));
    }
}
